//
//  ChoiceDateTimeController.m
//  RuiHeApp
//
//  Created by Jun on 2023/11/23.
//

#import "ChoiceDateTimeController.h"
#import "RHWindowTipManage.h"
#import "UIView+Extension.h"

@interface ChoiceDateTimeController ()

@property (nonatomic ,strong) UIView *bgView;

@property (nonatomic ,strong) UIDatePicker *datePicker;

@end

@implementation ChoiceDateTimeController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI{
    
    [self.view setBackgroundColor:[UIColor.blackColor colorWithAlphaComponent:0]];
    
    UIView *touchView = [[UIView alloc] init];
    [touchView setBackgroundColor:UIColor.clearColor];
    [self.view addSubview:touchView];
    
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, kUI_WINDOW_H, kUI_WINDOW_W, (216+50+ kTabbarSafeBottomMargin))];
    [_bgView setBackgroundColor:UIColor.whiteColor];
    [self.view addSubview:_bgView];
    
    QMUILabel *lab = [[QMUILabel alloc] init];
    [lab setText:@"请选择".rh_localized];
    [lab setTextAlignment:NSTextAlignmentLeft];
    [lab setFont:[UIFont boldSystemFontOfSize:16]];
    [lab setTextColor:FontColor(@"#666666")];
    [_bgView addSubview:lab];
    
    QMUIButton *btn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"确定".rh_localized forState:UIControlStateNormal];
    [btn setBackgroundColor:[FontColor(@"#000000") colorWithAlphaComponent:0.9]];
    [btn.layer setCornerRadius:5];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [btn setTitleColor:FontColor(@"ffffff") forState:UIControlStateNormal];
    [_bgView addSubview:btn];
    
    
    UIView *lineV = [[UIView alloc] init];
    [lineV setBackgroundColor:FontColor(@"#eeeeee")];
    [_bgView addSubview:lineV];
    
    
    _datePicker = [[UIDatePicker alloc] init];
    _datePicker.datePickerMode = UIDatePickerModeDateAndTime;
    [_datePicker setDate:[NSDate date]];
    
    //_datePicker.datePickerMode = UIDatePickerModeTime;
    _datePicker.calendar = [NSCalendar currentCalendar];
    _datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
    _datePicker.timeZone =[NSTimeZone systemTimeZone];
    
    [_bgView addSubview:_datePicker];
    if(@available(iOS 13.4, *)) {
        _datePicker.preferredDatePickerStyle = UIDatePickerStyleWheels;
    }else{
       // Fallback on earlier versions
    }
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    touchView.userInteractionEnabled = YES;
    [touchView addGestureRecognizer:tap];
    
    [touchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(20);
        make.height.mas_equalTo(49);
        make.right.mas_offset(-60);
        make.top.mas_offset(0);
    }];
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(80, 40));
        make.centerY.equalTo(lab);
        make.right.mas_offset(-20);
    }];
    
    [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(0);
        make.right.mas_offset(0);
        make.height.mas_equalTo(1);
        make.top.equalTo(lab.mas_bottom);
    }];
    
    [_datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.bottom.mas_offset(-kTabbarSafeBottomMargin);
        make.height.mas_equalTo(216);
    }];

    
    [_bgView round:10 RectCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
    
    kWeakSelf;
    [tap.rac_gestureSignal subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
        [weakSelf hidden];
    }];
    
    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf confirmAction];
    }];
    
}


- (void)hidden{
    [UIView animateWithDuration:0.5 animations:^{
        [self->_bgView setFrame:CGRectMake(0,kUI_WINDOW_H, kUI_WINDOW_W, 216+50+kTabbarSafeBottomMargin)];
        [self.view setBackgroundColor:[UIColor.blackColor colorWithAlphaComponent:0]];
    } completion:^(BOOL finished) {

        [[RHWindowTipManage shareWindowTipManage] hidden];
    }];
}


- (void)showAnimation{
    [UIView animateWithDuration:0.5 animations:^{
        [self->_bgView setFrame:CGRectMake(0,kUI_WINDOW_H-(216+50+kTabbarSafeBottomMargin), kUI_WINDOW_W, 216+50+kTabbarSafeBottomMargin)];
        [self.view setBackgroundColor:[UIColor.blackColor colorWithAlphaComponent:0.3]];
    }];
}


- (void)confirmAction{
    if(self.resultCallBack){
        self.resultCallBack(self.datePicker.date);
    }
    [self hidden];
}


- (void)hiddenAnimation{
    
}


-(void)dealloc{
    
}
@end
